package org.gdkm.el;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

import org.gdkm.el.elt.AIEF;
import org.gdkm.el.elt.DataPack;

/**
 * 实现层类. </br>
 * 用于减低的程序的耦合性. </br>
 * 日期: 2012-10-13
 * <p>
 * 
 * @author Arylo<<b>arylo.open@gmail.com</b>>
 * @version 1.0
 */
public class TranspThread implements AIEF {
	private static boolean isConnected = false;
	private static boolean isConnecting = false;
	private static Socket s = null;
	private static ObjectInputStream oi = null;
	private static ObjectOutputStream oo = null;
	private static String local_ip = null;
	private static Thread autoInputThread = null;
	private static Thread autoOutputthread = null;
	private static DataPack dp = null;

	/**
	 * 构建一个TranspThread对象.
	 */
	public TranspThread() {

	}

	private static void checkSocket(String ip, int port) {
		final String _ip = ip;
		final int _port = port;
		new Thread() {
			@Override
			public void run() {
				try {
					Socket socket = new Socket(_ip, _port);
					if (socket.isConnected() == true) {
						isConnected = true;
						s = socket;
						s.setSoTimeout(1000 * 60);
						startSocket();
					}
				} catch (Exception e) {
				}
			}
		}.start();
	}
	
	/**
	 * 尝试向ip 的port 端口连接Socket.
	 * 
	 * @param ip IP地址
	 * @param port 端口数
	 */
	public static void reSocket(String ip, int port) {
		checkSocket(ip, port);
	}
	
	/**
	 * 尝试向ip 的8423端口连接Socket.
	 * 
	 * @param ip IP地址
	 */
	public static void reSocket(String ip) {
		checkSocket(ip, 8423);
	}
	
	/**
	 * 尝试向本机IP网段的port 端口连接Socket.
	 * 
	 * @param port 端口数
	 */
	public static void reSocket(int port) {
		try {
			local_ip = InetAddress.getLocalHost().getHostAddress().toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		local_ip = local_ip.substring(0, local_ip.lastIndexOf(".") + 1);
		for (int i = 1; i < 255; i++) {
			if (isConnected != true) {
				checkSocket(local_ip + i, port);
			}
		}
	}
	
	/**
	 * 尝试向本机IP网段的8423端口连接Socket.
	 */
	public static void reSocket() {
		reSocket(8423);
	}

	/**
	 * 运行SocketThread连接.
	 */
	private static void startSocket() {
		try {
			if (oo == null) {
				oo = new ObjectOutputStream(s.getOutputStream());

			}
			if (oi == null) {
				oi = new ObjectInputStream(s.getInputStream());

			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (autoOutputthread == null) {
			autoOutputthread = new Thread() {
				@Override
				public void run() {
					while (true) {
						try {
							Thread.sleep(1000);
							oo.writeObject(new DataPack(SOCKET_ONLINE));
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			};
		}
		if (autoInputThread == null) {
			autoInputThread = new Thread() {
				@Override
				public void run() {
					try {
						Thread.sleep(1000);
						while (true) {
							while ((dp = (DataPack) oi.readObject()) != null) {
								if (dp.getIde().equals(SOCKET_AIEF)) {
									String text = dp.getText();
								}
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			};
		}
		autoOutputthread.start();
		autoInputThread.start();
	}

	/**
	 * 停止Socket连接.
	 */
	public static void stopSocket() {
		autoOutputthread.stop();
		autoInputThread.stop();
	}

	/**
	 * 往Socket 写数据.
	 * 
	 * @param text
	 *            识别的代码
	 */
	public static void writeSocket(String text) {
		writeSocket(text, null);
	}

	/**
	 * 往Socket 写数据(带文件).
	 * 
	 * @param text
	 *            识别的代码
	 * @param object
	 *            附带的文件
	 */
	public static void writeSocket(String text, Object object) {
		if (isConnecting == true && oo != null) {
			try {
				oo.writeObject(new DataPack(text, object));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 返回Socket是否正在连接.
	 */
	public static boolean isConnecting() {
		return isConnecting;
	}

	/**
	 * 返回Socket是否已连接.
	 */
	public static boolean isConnected() {
		return isConnected;
	}

}
